﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace jsweb
{
   class MVC
   {
      public Dictionary<string, string> GetActions { get; set; }
      public Dictionary<string, string> PostActions { get; set; }

      public MVC()
      {
         GetActions = new Dictionary<string, string>();
         PostActions = new Dictionary<string, string>();

         // Find controllers
         foreach (var file in Directory.EnumerateFiles("Controllers", "*.js"))
         {
            // Parse each controller for GET-requests
            var regex = new Regex("<get (.*?)>(.*?)</get>", RegexOptions.Singleline);
            foreach (Match match in regex.Matches(File.ReadAllText(file)))
            {
               // Find function name
               var mapping = match.Groups[1].Value.Replace("[", "").Replace("]", "");
               var function = match.Groups[2].Value;
               GetActions.Add(mapping, function);
            }

            // Parse each controller for POST-requests
            regex = new Regex("<post (.*?)>(.*?)</post>", RegexOptions.Singleline);
            foreach (Match match in regex.Matches(File.ReadAllText(file)))
            {
               // Find function name
               var mapping = match.Groups[1].Value.Replace("[", "").Replace("]", "");
               var function = match.Groups[2].Value;
               PostActions.Add(mapping, function);
            }
         }
      }
   }
}
